home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / qe4 / drag.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-12  |  8.3 KB  |  436 lines

  1. #include "qe3.h"
  2.  
  3. /*
  4.  
  5.   drag either multiple brushes, or select plane points from
  6.   a single brush.
  7.  
  8. */
  9.  
  10. qboolean    drag_ok;
  11. vec3_t    drag_xvec;
  12. vec3_t    drag_yvec;
  13.  
  14. static    int    buttonstate;
  15. static    int    pressx, pressy;
  16. static    vec3_t    pressdelta;
  17. static    int    buttonx, buttony;
  18.  
  19.  
  20. //int        num_move_points;
  21. //float    *move_points[1024];
  22.  
  23. int        lastx, lasty;
  24.  
  25. qboolean    drag_first;
  26.  
  27.  
  28. void    AxializeVector (vec3_t v)
  29. {
  30.     vec3_t    a;
  31.     float    o;
  32.     int        i;
  33.  
  34.     if (!v[0] && !v[1])
  35.         return;
  36.     if (!v[1] && !v[2])
  37.         return;
  38.     if (!v[0] && !v[2])
  39.         return;
  40.  
  41.     for (i=0 ; i<3 ; i++)
  42.         a[i] = fabs(v[i]);
  43.     if (a[0] > a[1] && a[0] > a[2])
  44.         i = 0;
  45.     else if (a[1] > a[0] && a[1] > a[2])
  46.         i = 1;
  47.     else
  48.         i = 2;
  49.  
  50.     o = v[i];
  51.     VectorCopy (vec3_origin, v);
  52.     if (o<0)
  53.         v[i] = -1;
  54.     else
  55.         v[i] = 1;
  56.     
  57. }
  58.  
  59.  
  60. /*
  61. ===========
  62. Drag_Setup
  63. ===========
  64. */
  65. void Drag_Setup (int x, int y, int buttons,
  66.            vec3_t xaxis, vec3_t yaxis,
  67.            vec3_t origin, vec3_t dir)
  68. {
  69.     trace_t    t;
  70.     face_t    *f;
  71.  
  72.     if (selected_brushes.next == &selected_brushes)
  73.     {
  74.         Sys_Status("No selection to drag\n", 0);
  75.         return;
  76.     }
  77.  
  78.     drag_first = true;
  79.     g_qeglobals.d_num_move_points = 0;
  80.     VectorCopy (vec3_origin, pressdelta);
  81.     pressx = x;
  82.     pressy = y;
  83.  
  84.     VectorCopy (xaxis, drag_xvec);
  85.     AxializeVector (drag_xvec);
  86.     VectorCopy (yaxis, drag_yvec);
  87.     AxializeVector (drag_yvec);
  88.  
  89.     if (g_qeglobals.d_select_mode == sel_vertex)
  90.     {
  91.         SelectVertexByRay (origin, dir);    
  92.         if (g_qeglobals.d_num_move_points)
  93.         {
  94.             drag_ok = true;
  95.             return;
  96.         }
  97.     }
  98.     if (g_qeglobals.d_select_mode == sel_edge)
  99.     {
  100.         SelectEdgeByRay (origin, dir);    
  101.         if (g_qeglobals.d_num_move_points)
  102.         {
  103.             drag_ok = true;
  104.             return;
  105.         }
  106.     }
  107.  
  108.  
  109.     //
  110.     // check for direct hit first
  111.     //
  112.     t = Test_Ray (origin, dir, true);
  113.     if (t.selected)
  114.     {
  115.         drag_ok = true;
  116.  
  117.         if (buttons == (MK_LBUTTON|MK_CONTROL) )
  118.         {
  119.             Sys_Printf ("Shear dragging face\n");
  120.             Brush_SelectFaceForDragging (t.brush, t.face, true);
  121.         }
  122.         else if (buttons == (MK_LBUTTON|MK_CONTROL|MK_SHIFT) )
  123.         {
  124.             Sys_Printf ("Sticky dragging brush\n");
  125.             for (f=t.brush->brush_faces ; f ; f=f->next)
  126.                 Brush_SelectFaceForDragging (t.brush, f, false);
  127.         }
  128.         else
  129.             Sys_Printf ("Dragging entire selection\n");
  130.         
  131.         return;
  132.     }
  133.  
  134.     if (g_qeglobals.d_select_mode == sel_vertex || g_qeglobals.d_select_mode == sel_edge)
  135.         return;
  136.  
  137.     //
  138.     // check for side hit
  139.     //
  140.     if (selected_brushes.next->next != &selected_brushes)
  141.     {
  142.         Sys_Printf ("Click isn't inside multiple selection\n");
  143.         return;
  144.     }
  145.  
  146.     if (selected_brushes.next->owner->eclass->fixedsize)
  147.     {
  148.         Sys_Printf ("Can't stretch fixed size entities\n");
  149.         return;
  150.     }
  151.  
  152.  
  153.     if (buttons & MK_CONTROL)
  154.         Brush_SideSelect (selected_brushes.next, origin, dir, true);
  155.     else
  156.         Brush_SideSelect (selected_brushes.next, origin, dir, false);
  157.  
  158.  
  159.     Sys_Printf ("Side stretch\n");
  160.     drag_ok = true;
  161. }
  162.  
  163. entity_t *peLink;
  164.  
  165. void UpdateTarget(vec3_t origin, vec3_t dir)
  166. {
  167.     trace_t    t;
  168.     entity_t *pe;
  169.     int i;
  170.     char sz[128];
  171.  
  172.     t = Test_Ray (origin, dir, 0);
  173.  
  174.     if (!t.brush)
  175.         return;
  176.  
  177.     pe = t.brush->owner;
  178.  
  179.     if (pe == NULL)
  180.         return;
  181.  
  182.     // is this the first?
  183.     if (peLink != NULL)
  184.     {
  185.  
  186.         // Get the target id from out current target
  187.         // if there is no id, make one
  188.  
  189.         i = IntForKey(pe, "target");
  190.         if (i <= 0)
  191.         {
  192.             i = GetUniqueTargetId(1);
  193.             sprintf(sz, "%d", i);
  194.  
  195.             SetKeyValue(pe, "target", sz);
  196.         }
  197.  
  198.         // set the target # into our src
  199.  
  200.         sprintf(sz, "%d", i);
  201.         SetKeyValue(peLink, "targetname", sz);
  202.  
  203.         Sys_UpdateWindows(W_ENTITY);
  204.  
  205.     }
  206.  
  207.     // promote the target to the src
  208.  
  209.     peLink = pe;
  210.     
  211. }
  212.  
  213. /*
  214. ===========
  215. Drag_Begin
  216. ===========
  217. */
  218. void Drag_Begin (int x, int y, int buttons,
  219.            vec3_t xaxis, vec3_t yaxis,
  220.            vec3_t origin, vec3_t dir)
  221. {
  222.     trace_t    t;
  223.  
  224.     drag_ok = false;
  225.     VectorCopy (vec3_origin, pressdelta);
  226.  
  227.     drag_first = true;
  228.     peLink = NULL;
  229.  
  230.     // shift LBUTTON = select entire brush
  231.     if (buttons == (MK_LBUTTON | MK_SHIFT))
  232.     {
  233.         if (!dir[0] && !dir[1])
  234.             Select_Ray (origin, dir, SF_ENTITIES_FIRST);    // hack for XY
  235.         else
  236.             Select_Ray (origin, dir, 0);
  237.         return;
  238.     }
  239.  
  240.     // ctrl-shift LBUTTON = select single face
  241.     if (buttons == (MK_LBUTTON | MK_CONTROL | MK_SHIFT))
  242.     {
  243.         Select_Deselect ();
  244.         Select_Ray (origin, dir, SF_SINGLEFACE);
  245.         return;
  246.     }
  247.  
  248.     // LBUTTON + all other modifiers = manipulate selection
  249.     if (buttons & MK_LBUTTON)
  250.     {
  251.         Drag_Setup (x, y, buttons, xaxis, yaxis, origin, dir);
  252.         return;
  253.     }
  254.  
  255.     // middle button = grab texture
  256.     if (buttons == MK_MBUTTON)
  257.     {
  258.         t = Test_Ray (origin, dir, false);
  259.         if (t.face)
  260.         {
  261.             g_qeglobals.d_new_brush_bottom_z = t.brush->mins[2];
  262.             g_qeglobals.d_new_brush_top_z = t.brush->maxs[2];
  263.             Texture_SetTexture (&t.face->texdef);
  264.         }
  265.         else
  266.             Sys_Printf ("Did not select a texture\n");
  267.         return;
  268.     }
  269.  
  270.     // ctrl-middle button = set entire brush to texture
  271.     if (buttons == (MK_MBUTTON|MK_CONTROL) )
  272.     {
  273.         t = Test_Ray (origin, dir, false);
  274.         if (t.brush)
  275.         {
  276.             if (t.brush->brush_faces->texdef.name[0] == '(')
  277.                 Sys_Printf ("Can't change an entity texture\n");
  278.             else
  279.             {
  280.                 Brush_SetTexture (t.brush, &g_qeglobals.d_texturewin.texdef);
  281.                 Sys_UpdateWindows (W_ALL);
  282.             }
  283.         }
  284.         else
  285.             Sys_Printf ("Didn't hit a btrush\n");
  286.         return;
  287.     }
  288.  
  289.     // ctrl-shift-middle button = set single face to texture
  290.     if (buttons == (MK_MBUTTON|MK_SHIFT|MK_CONTROL) )
  291.     {
  292.         t = Test_Ray (origin, dir, false);
  293.         if (t.brush)
  294.         {
  295.             if (t.brush->brush_faces->texdef.name[0] == '(')
  296.                 Sys_Printf ("Can't change an entity texture\n");
  297.             else
  298.             {
  299.                 t.face->texdef = g_qeglobals.d_texturewin.texdef;
  300.                 Brush_Build( t.brush );
  301.                 Sys_UpdateWindows (W_ALL);
  302.             }
  303.         }
  304.         else
  305.             Sys_Printf ("Didn't hit a btrush\n");
  306.         return;
  307.     }
  308.  
  309. }
  310.  
  311.  
  312. /*
  313. ===========
  314. MoveSelection
  315. ===========
  316. */
  317. void MoveSelection (vec3_t move)
  318. {
  319.     int        i;
  320.     brush_t    *b;
  321.  
  322.     if (!move[0] && !move[1] && !move[2])
  323.         return;
  324.  
  325.     Sys_UpdateWindows (W_XY|W_CAMERA);
  326.  
  327.     //
  328.     // dragging only a part of the selection
  329.     //
  330.     if (g_qeglobals.d_num_move_points)
  331.     {
  332.         for (i=0 ; i<g_qeglobals.d_num_move_points ; i++)
  333.             VectorAdd (g_qeglobals.d_move_points[i], move, g_qeglobals.d_move_points[i]);
  334.  
  335.         for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  336.         {
  337.             Brush_Build( b );
  338.             for (i=0 ; i<3 ; i++)
  339.                 if (b->mins[i] > b->maxs[i]
  340.                 || b->maxs[i] - b->mins[i] > 4096)
  341.                     break;    // dragged backwards or fucked up
  342.             if (i != 3)
  343.                 break;
  344.         }
  345.  
  346.         // if any of the brushes were crushed out of existance
  347.         // calcel the entire move
  348.         if (b != &selected_brushes)
  349.         {
  350.             Sys_Printf ("Brush dragged backwards, move canceled\n");
  351.             for (i=0 ; i<g_qeglobals.d_num_move_points ; i++)
  352.                 VectorSubtract (g_qeglobals.d_move_points[i], move, g_qeglobals.d_move_points[i]);
  353.  
  354.             for (b=selected_brushes.next ; b != &selected_brushes ; b=b->next)
  355.                 Brush_Build( b );
  356.         }
  357.  
  358.     }
  359.     else
  360.     {
  361.         //
  362.         // if there are lots of brushes selected, just translate instead
  363.         // of rebuilding the brushes
  364.         //
  365.         if (drag_yvec[2] == 0 && selected_brushes.next->next != &selected_brushes)
  366.         {
  367.             VectorAdd (g_qeglobals.d_select_translate, move, g_qeglobals.d_select_translate);
  368.         }
  369.         else
  370.         {
  371.             Select_Move (move);
  372.         }
  373.     }
  374. }
  375.  
  376. /*
  377. ===========
  378. Drag_MouseMoved
  379. ===========
  380. */
  381. void Drag_MouseMoved (int x, int y, int buttons)
  382. {
  383.     vec3_t    move, delta;
  384.     int        i;
  385.     char    movestring[128];
  386.  
  387.     if (!buttons)
  388.     {
  389.         drag_ok = false;
  390.         return;
  391.     }
  392.     if (!drag_ok)
  393.         return;
  394.  
  395.     // clear along one axis
  396.     if (buttons & MK_SHIFT)
  397.     {
  398.         drag_first = false;
  399.         if (abs(x-pressx) > abs(y-pressy))
  400.             y = pressy;
  401.         else
  402.             x = pressx;
  403.     }
  404.  
  405.  
  406.     for (i=0 ; i<3 ; i++)
  407.     {
  408.         move[i] = drag_xvec[i]*(x - pressx) 
  409.                 + drag_yvec[i]*(y - pressy);
  410.         move[i] = floor(move[i]/g_qeglobals.d_gridsize+0.5)*g_qeglobals.d_gridsize;
  411.     }
  412.  
  413.     sprintf (movestring, "drag (%i %i %i)", (int)move[0], (int)move[1], (int)move[2]);
  414.     Sys_Status (movestring, 0);
  415.  
  416.     VectorSubtract (move, pressdelta, delta);
  417.     MoveSelection (delta);
  418.     VectorCopy (move, pressdelta);
  419. }
  420.  
  421. /*
  422. ===========
  423. Drag_MouseUp
  424. ===========
  425. */
  426. void Drag_MouseUp (void)
  427. {
  428.     Sys_Status ("drag completed.", 0);
  429.     if (g_qeglobals.d_select_translate[0] || g_qeglobals.d_select_translate[1] || g_qeglobals.d_select_translate[2])
  430.     {
  431.         Select_Move (g_qeglobals.d_select_translate);
  432.         VectorCopy (vec3_origin, g_qeglobals.d_select_translate);
  433.         Sys_UpdateWindows (W_CAMERA);
  434.     }
  435. }
  436.